package com.niannian.dialog;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.niannian.BaseActivity;
import com.niannian.MyApplication;
import com.niannian.util.VAR;
import com.niannian.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SelectPhotoDialog extends Dialog {
	private Context context;
	private TextView photograph;
	private TextView album;
	private TextView cancel;
	public static String PHOTONAME = "headtemp.jpg";
	public static final String IMAGE_UNSPECIFIED = "image/*";
	private File photoSavePath;

	public SelectPhotoDialog(Context context) {
		super(context);
		this.context = context;
	}

	public SelectPhotoDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
	}

	public SelectPhotoDialog(Context context, int theme, File path) {
		super(context, theme);
		this.context = context;
		this.photoSavePath = path;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addphotos1);
		initView();
		setLisenter();
	}

	private void initView() {
		cancel = (TextView) findViewById(R.id.btn_cancel);
		photograph = (TextView) findViewById(R.id.tv_NowFilming);
		album = (TextView) findViewById(R.id.tv_Album);

	}

	private void setLisenter() {

		cancel.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					dismiss();
				}
				return true;
			}
		});

		photograph.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.i(VAR.LEVEL_INFO, photoSavePath.toString());
					Intent intent = getTakePickIntent(photoSavePath);
					((BaseActivity) context).startActivityForResult(intent, VAR.PHOTO_HRAPH);
					dismiss();
				}
				return true;
			}
		});

		album.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.d("选择照片", "选择照片");
					Intent intent = new Intent(Intent.ACTION_PICK, null);
					intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
					((BaseActivity) context).startActivityForResult(intent, VAR.PHOTO_ALBUM);
					dismiss();
				}

				return true;
			}
		});

	}

	private Intent getTakePickIntent(File f) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
		return intent;
	}

	@SuppressLint("SimpleDateFormat")
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".jpg";
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		dismiss();
		return super.onTouchEvent(event);
	}

}
